package com.wenmingliang;

import com.wenmingliang.demo1.Person;
import org.junit.Test;

import java.io.*;
import java.util.Properties;

public class ObjectInputStreamTest {

    @Test
    public void testObjectOutputStream() {

        ObjectOutputStream objectOutputStream = null;
        try {
            objectOutputStream = new ObjectOutputStream(new FileOutputStream("object.dat"));
            objectOutputStream .writeObject(new String("fsdjkfsa"));
            objectOutputStream.flush();
            objectOutputStream.writeObject(new Person("wangming", 23));
            objectOutputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (objectOutputStream != null) {
                try {
                    objectOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }

    }

    @Test
    public void testObjectInputStream() {
        ObjectInputStream objectInputStream = null;
        try {
            objectInputStream = new ObjectInputStream(new FileInputStream("object.dat"));
            Object o = objectInputStream.readObject();
            String str = (String) o;
            Person p = (Person) objectInputStream.readObject();
            System.out.println(str);
            System.out.println(p);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (objectInputStream != null) {
                try {
                    objectInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }

}